package com.amlyc.syn;

public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket,"a").start();
        new Thread(buyTicket,"b").start();
        new Thread(buyTicket,"c").start();
    }
}
class BuyTicket implements Runnable{
    private int ticketNums = 100;
    boolean flag = true;
    @Override
    public void run() {
        while (flag) {
            buy();
        }
    }

    private synchronized void buy(){
        if (ticketNums <= 0) {
            flag = false;
            return;
        }

        System.out.println(Thread.currentThread().getName()+"-->"+ticketNums--);

    }
}